// pages/advice/advice.js
const util = require('../../utils/util.js');
const timePicker = require('../../utils/timePicker.js');
const app=getApp();
Page({
 
  /** 页面的初始数据*/
  data: {
    content:'',
    distance:'',
    name:'',
    advice:'',
    pics:[],
    isShow: true,
    clocked:0,
    title:'',
    //startDate: util.formatDate(dateNow),
    //startTime: util.formatTime(dateNow),
    //endDate: util.formatDate(dateNow),
    //endTime: util.formatTime(dateNow),
    startTimeArray: null,
    endTimeArray:null,
  },
  onLoad: function (options) {
  
    // 获取时分秒数组，在页面中显示
    var obj = timePicker.timePicker();
    this.setData({
      platform:app.globalData.platform,
     // city:app.globalData.city,
       width:app.globalData.windowWidth,
      height:app.globalData.windowHeight,
      statusBarHeight:app.globalData.statusBarHeight,
      navHeight:app.globalData.navHeight,
      marginTop:app.globalData.navTitleTop,
      domain:app.globalData.domain
    })
    this.setData({
      startTimeArray: obj.timeArray,
      startTime:obj.time,
    });

  },
  goBack:function(){
    wx.navigateBack();
  },
  startTimeColumn(e) {
    var startTimeArr = this.data.startTime;
    startTimeArr[e.detail.column] = e.detail.value;
    this.setData({
      startTime: startTimeArr
    });

  },
  startTimeChange: function (e) {
    var startTimeArr = this.data.startTime;
    startTimeArr[e.detail.column] = e.detail.value;
    this.setData({
      startTime: startTimeArr
    });
    
  },
 
  /**获取textarea值：评论内容 */
  bindTextAreaBlur:function(e){
    this.setData({
      advice:e.detail.value
    })
  },
  titleBlur:function(e){
    this.setData({
      title:e.detail.value
    })
  },
  distanceBlur:function(e){
    this.setData({
      distance:e.detail.value
    })
  },
  getPosition:function(){
    wx.navigateTo({
      url: '../map/index',
    })
  },
 
 
  /**上传图片 */
  uploadImage:function(){
    let that=this;
    let pics = that.data.pics;
    var n = 9;
    if (9 > pics.length > 0) {
      n = 9 - pics.length;
    } else if (pics.length == 9) {
      n = 1;
    }
    wx.chooseImage({
      count:9,
      sizeType: ['original', 'compressed'], 
      sourceType: ['album', 'camera'], 
      success: function(res) {
        let imgSrc = res.tempFilePaths;
        if(imgSrc.length>1){
          imgSrc.forEach(element => {
            pics.push(element);
            console.log(element);
          });
        }else{
          console.log(imgSrc["0"]);
          pics.push(imgSrc["0"]);
        }
         
        if (pics.length >= 9){
          that.setData({
            isShow: false
          }) 
        }
       that.setData({
          pics: pics
        })
      },
    })
 
  },
 
  /**删除图片 */
  deleteImg:function(e){
    let that=this;
    let deleteImg=e.currentTarget.dataset.img;
    
    let pics = that.data.pics;
    let newPics=[];
    for (let i = 0;i<pics.length; i++){
      //console.log(pics[i]);
     //判断字符串是否相等
      if (pics[i]!== deleteImg){
        //console.log(pics[i]);
        newPics.push(pics[i]);
      }
    }
    that.setData({
      pics: newPics,
      isShow: true
    })
    
  },
 
  /**提交 */
  submitAdvice:function(){
    let that=this;
    var data=this.data;
    let advice = data.advice;
    let distance=data.distance;
    var open=wx.getStorageSync('open');
    var member_id=open.member_id;
    let pics=data.pics;
    let timeArray=data.startTimeArray;
    let stratTime=data.startTime
    let time=timeArray[0][stratTime[0]]+':'+timeArray[1][stratTime[1]]+':'+timeArray[2][stratTime[2]];
    var position=data.position;
    var title=data.title;
    //console.log(timeArray);
    //console.log();
    //保存操作
    if(!distance){
      wx.showToast({
        title: '距离不能为空',
      })
      return;
    }
    console.log(time);
    wx.request({
      url: app.globalData.domain+'member/clock/clock',
      data:{ title:title,distance:distance,advice:advice,time:time,member_id: member_id,position:position},
      success:function(res){
        if(res.data.clock_state=='success'){
          var clock_id=res.data.clock_id;
          //console.log(res.data);
          if(clock_id){
            pics.forEach(element => {
              wx.uploadFile({
                filePath: element,
                name: 'file',
                url: app.globalData.domain+'member/clock/clockImages',
                header: { 
                  'Content-Type': 'multipart/form-data'
                  //'Authorization': wx.getStorageSync("access_token"),  //如果需要token的话要传
                },
                formData: {
                  clock_id:clock_id,
                  member_id: member_id
                },
                success:function(res){
                  var data = JSON.parse(res.data);
                  console.log(data);
                  wx.showModal({
                   title:'打卡成功',
                   content:data.image_msg,
                   showCancel:false,
                   success:function(res){
                    if (res.confirm) {
                       //console.log('用户点击确定')
                        wx.navigateBack();
                      } 
                      
                   }
                  })
                }
              })
            });
          }else{
            wx.reLaunch({
              url: 'index',
            })
          }
        }
      }
    })

    
  }
})